package sixth.抽象类与接口的应用.设计模式.工厂设计;


import java.util.Scanner;
interface Fruit{
    public void eat();
}
// Fruit接口就不写了
class Apple implements Fruit {
    @Override
    public void eat() {
        System.out.println("吃苹果！");
    }
}

class Orange implements Fruit {
    @Override
    public void eat() {
        System.out.println("吃橘子！");
    }
}

class Factory {// 这时候的工厂类就相当于一个过渡端 想要什么类直接get一下就行 不用修改主方法了

    // public static Fruit getInstance(Fruit fruit) {
    public static Fruit getInstance(String className) {
        Fruit fruit = null;

        if ("apple".equals(className)) {
            fruit = new Apple();
        }
        if ("orange".equals(className)) {
            fruit = new Orange();
        }
        return fruit;
    }
}

public class 工厂设计模式 {
    public static void main(String[] args) {

        System.out.println("请输入一个类名：");
        Scanner scanner = new Scanner(System.in);
        String claname = scanner.nextLine();//消耗掉回车符
        Fruit f = Factory.getInstance(claname);
        if(f!=null){
            f.eat();// 吃xx
        }
        scanner.close();
    }
}
/*
D:\WorkSpaces\Java\jdk-21\bin\java.exe "-javaagent:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=62957:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\WorkSpaces\JavaDevelopBase_start_329\out\production\JavaDevelopBase_start_329 sixth.抽象类与接口的应用.设计模式__工厂设计.工厂设计模式
请输入一个类名：
apple
吃苹果！

Process finished with exit code 0

 */